<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Pushmsg extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;
    public $funLists = array(
        '0' => '无',
        '1' => 'URL',
        '2' => '自定义功能',
    );

    public function _initialize()
    {
        (new \app\admin\behavior\CheckPermissions())->run();//增加权限控制
        $this->_model = new \app\admin\model\PushMsg();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        $this->_mapper = \app\admin\mapper\Pushmsg::getInstance();
        $this->_mapper->setFunLists($this->funLists);
        $this->assign('funLists', $this->funLists);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth
        $ser_fun = trim(input("request.ser_fun"));//auth
        $ser_push_time_type = trim(input("request.ser_push_time_type"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }
        if ($ser_stats == '0' || $ser_stats == "1") {
            $where['stats'] = $ser_stats;
        }
        if ($ser_fun == '0' || $ser_fun > "0") {
            $where['fun'] = $ser_fun;
        }

        if ($ser_push_time_type == '0' || $ser_push_time_type > "0") {
            $where['push_time_type'] = $ser_push_time_type;
        }


        $where['is_delete'] = '0';
        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = $this->_mapper->listMapper($lists['data']);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $push_time_type = $_REQUEST['push_time_type'];
            $ds_push_time1 = $_REQUEST['ds_push_time1'];
            $ds_push_time2 = $_REQUEST['ds_push_time2'];
            $push_time_val = "";
            if ($push_time_type == 1) {
                $push_time_val = $ds_push_time1;
            } else if ($push_time_type == 2) {
                $push_time_val = $ds_push_time2;
            }
            $data1 = $_REQUEST['data'];
            $data2 = array(
                'ctime' => date("Y-m-d H:i:s"),
                'push_time_type' => $push_time_type,
                'push_time_val' => $push_time_val,
            );
            $data = array_merge($data1, $data2);
            $scop = $_REQUEST['scop'];
            $uname = $_REQUEST['uname'];


            $userList = array();
            if ($scop == 0)//scop 0全部 1普通用户 2vip用户  3单个用户
            {
                $addResult = addPushmsgForAll(0, $data);//ty 0所有用户  1非vip用户 2vip用户
            } else if ($scop == 1)//scop 0全部 1普通用户 2vip用户  3单个用户
            {
                $addResult = addPushmsgForAll(1, $data);//ty 0所有用户  1非vip用户 2vip用户

            } else if ($scop == 2)//scop 0全部 1普通用户 2vip用户  3单个用户
            {
                $addResult = addPushmsgForAll(2, $data);//ty 0所有用户  1非vip用户 2vip用户

            } else if ($scop == 3)//scop 0全部 1普通用户 2vip用户  3单个用户
            {
                $tempUserInfo = getUserInfoForUname($uname, 'id,uname,nickname');
                if (empty($tempUserInfo)) {
                    $return = ['err' => 1, 'msg' => '操作失败，用户不存在'];
                    return json($return);
                }
                $data['uid'] = $tempUserInfo['id'];
                $addResult = addPushmsgForSingle($data);
            }
            if($addResult['err']==0) {
                if ($data['push_time_type'] == 0)//立即推送
                {
                    $allInsertId = $addResult['allInsertId'];
                    foreach ($allInsertId as $key => $val) {
                        addPushMsgQueue($val);//添加app推送队列
                    }
                }
            }
            return json($addResult);
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }

    public function setAvail()
    {
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_avail_push' => 0), $id);

        if ($res || $res === 0) {
            addPushMsgQueue($id);//添加app推送队列
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }
}